package com.lianheng.cms.common.gen;

import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.generator.BaseModelGenerator;
import com.jfinal.plugin.activerecord.generator.Generator;

import javax.sql.DataSource;

/**
 * 生成jfinal model模板
 **/
public class JFinalModelGenerator extends JFinalConfig {
    public static void main(String[] args) {
        JFinalModelGenerator jFinalGenerator = new JFinalModelGenerator();
        jFinalGenerator.genModel(dataSource);
        jFinalGenerator.genSwaggerDto(dataSource);
    }

    public void genModel(DataSource dataSource) {
        String baseDir = baseModelPackageName.replace(".", "\\");
        baseDir = PathKit.getWebRootPath() + projectType + baseDir;
        String modelDir = modelPackageName.replace(".", "\\");
        modelDir = PathKit.getWebRootPath() + projectType + modelDir;
        Generator gernerator = new Generator(dataSource, baseModelPackageName, baseDir,
                modelPackageName, modelDir);
        gernerator.setBaseModelTemplate(baseModelTemplate);
        gernerator.setGenerateDaoInModel(true);
        gernerator.setGenerateDataDictionary(false);
        gernerator.setMetaBuilder(new IMetaBuilder(dataSource));
        gernerator.setRemovedTableNamePrefixes(tablePrefix);


        String mappingDir = mappingKitPackageName.replace(".", "\\");
        mappingDir = PathKit.getWebRootPath() + projectType + mappingDir;
        gernerator.setMappingKitClassName(mappingKitClassName);
        gernerator.setMappingKitPackageName(mappingKitPackageName);
        gernerator.setMappingKitOutputDir(mappingDir);


        gernerator.generate();
    }

    public void genSwaggerDto(DataSource dataSource) {
        String dtoMappingDir = dtoPackageName.replace(".", "\\");
        dtoMappingDir = PathKit.getWebRootPath() + projectType + dtoMappingDir;
        BaseModelGenerator baseModelGenerator = new BaseModelGenerator(dtoPackageName, dtoMappingDir);
        Generator gernerator = new Generator(dataSource, baseModelGenerator);
        gernerator.setBaseModelTemplate(swaggerTemplate);
        gernerator.setGenerateDataDictionary(false);
        gernerator.setMetaBuilder(new MetaBuilderSwagger(dataSource));
        gernerator.setRemovedTableNamePrefixes(tablePrefix);
        gernerator.generate();
    }


}
